<?php
class Ialbum_Widget_MostLikedPhotosController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
  	$photoTable = Engine_Api::_()->getDbTable('photos', 'album');
  	$pName = $photoTable->info('name');
  	$likeTable  = Engine_Api::_()->getDbtable('likes', 'core');
  	$lName = $likeTable->info('name');
  	$albumTable = Engine_Api::_()->getItemTable('album');
  	$aName = $albumTable->info('name');
  	$select = $photoTable->select()
  				->from($pName)
  				->join($aName, $aName .'.album_id = ' .$pName. '.album_id', '')
  				->join($lName, $lName .'.resource_id = ' .$pName. '.photo_id', '')
  				->where($aName .'.search = ?', 1)
  				->where($lName. '.resource_type = ?' , 'album_photo')
  				->group($pName. '.photo_id')
  				->order("count($pName.photo_id) DESC");
	$this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 5));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
	if ($paginator->getTotalItemCount() <= 0) return $this->setNoRender();
  }
}